﻿/////////////////////////////////////////////////////////////////////////////////////////////
// <copyright file="Cleanup.cs" company="Microsoft Corporation">
//     Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
/////////////////////////////////////////////////////////////////////////////////////////////

namespace Microsoft.LiveAtEdu.Samples.Powershell.Cleanup
{
    using System.Management.Automation;

    /// <summary>
    /// Powershell action class to do cleanup activities.
    /// </summary>
    public class Cleanup
    {
        /// <summary>
        /// Session runspace instance.
        /// </summary>
        private readonly SessionRunspace sessionRunspace;

        /// <summary>
        /// Initializes a new instance of the Cleanup class.
        /// </summary>
        /// <param name="sessionRunspace">Session runspace instance.</param>
        public Cleanup(SessionRunspace sessionRunspace)
        {
            this.sessionRunspace = sessionRunspace;
        }

        /// <summary>
        /// Execute cleanup script. It is used to rollback domain to original state.
        /// </summary>
        /// <param name="script">Script to be executed.</param>
        /// <returns>Satus flag indicating the success of operation.</returns>
        /// <remarks>
        /// Usually this is called by the Demo portal to do cleanup activities.
        /// </remarks>
        public bool ExecuteSessionCleanupScript(string script)
        {
            try
            {
                var command = new PSCommand();
                command.AddScript(script);
                this.sessionRunspace.ExecuteCommand(command);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
